#include "mazewidget.h"
#include "switch.h"
#include <QPainter>
#include <QPaintEvent>
//涂色
QVector<int> currentPath;
MazeWidget *maze1;
MazeWidget::MazeWidget(QWidget *parent)
    : QWidget{parent}
{}
void MazeWidget::setMaze(const MazeData& mazeData)
{
    this->maze = mazeData.maze;   //迷宫
    this->start = mazeData.start; //入口
    this->end = mazeData.end;     //出口
    update();  // 更新绘制
};

void MazeWidget::paintEvent(QPaintEvent *event)
{
    Q_UNUSED(event);

    QPainter painter(this);

    int cellWidth = width() / maze[0].size();
    int cellHeight = height() / maze.size();

    // 绘制迷宫
    for (int i = 0; i < maze.size(); ++i) {
        for (int j = 0; j < maze[i].size(); ++j) {
            QRect rect(j * cellWidth, i * cellHeight, cellWidth, cellHeight);

            // 绘制黑色边框
            painter.setPen(Qt::black);
            painter.drawRect(rect);

            // 根据maze数组中的值填充不同颜色
            if (i == start.x() && j == start.y()) {
                painter.fillRect(rect, Qt::white);  // 入口，白色
            } else if (i == end.x() && j == end.y()) {
                painter.fillRect(rect, Qt::black);  // 出口，黑色
            } else if (maze[i][j] == 0) {
                painter.fillRect(rect, Qt::blue);  // 可通行区域，蓝色
            } else if(i == 0 || j == 0 || i == maze.size() - 1 || j == maze[i].size() - 1){
                painter.fillRect(rect, Qt::gray);  // 墙，灰色
            }
            else{
                painter.fillRect(rect, Qt::red);   //障碍物，红色
            }
        }
    }

    if(drawPath)
    {
        for(auto path:pR){
            painter.fillRect(path,Qt::blue);
        }
        int i=0;
        for(auto pa:pathRects){
            if(i==0){
                painter.fillRect(pa,Qt::green);
                i=1;
            }
            else if(i==1){
            painter.fillRect(pa,Qt::magenta);
                i=2;
            }
            else if(i==2){
                painter.fillRect(pa,Qt::yellow);
                i=3;
            }
            else if(i==3){
                painter.fillRect(pa,Qt::white);
                i=0;
            }
        }
        pathRects.resize(0);

    }
}
void MazeWidget::paintPath(QVector<int> path0,QVector<int> path)
{

    int cellWidth = width() / maze[0].size();
    int cellHeight = height() / maze.size();


    for (int k = 1; k < path.size()-1; ++k) {
        int pos = path[k];
        int row = hnode[pos]->xy.x();
        int col = hnode[pos]->xy.y();

        QRect rect(col * cellWidth, row * cellHeight, cellWidth, cellHeight);
        pathRects.append(rect);
        // pathRect=rect;
        drawPath=true;

        // painter1.setPen(Qt::green);
        // painter1.drawRect(rect);


    }
    for (int k = 1; k < path0.size()-1; ++k) {
        int pos = path0[k];
        int row = hnode[pos]->xy.x();
        int col = hnode[pos]->xy.y();

        QRect rect(col * cellWidth, row * cellHeight, cellWidth, cellHeight);
        pR.append(rect);
        // pathRect=rect;
        drawPath=true;

        // painter1.setPen(Qt::green);
        // painter1.drawRect(rect);


    }
    update();
}






